<?php

namespace App\Api;

use PhalApi\Api;


/**
 * 万国觉醒 登录
 * @author: dogstar <chanzonghuang@gmail.com> 2014-10-04
 */
class RokUesrWx extends Api
{

    public function getRules()
    {
        return array(
            "change_WX" => array(
                'username' => array('name' => 'username', 'require' => true, 'desc' => 'username'),
                'password' => array('name' => 'password', 'require' => true, 'desc' => 'password'),
                'openid'   => array('name' => 'openid', 'require' => true, 'desc' => 'openid'),
            ),
            "Login"     => array(
                'openid' => array('name' => 'openid', 'require' => true, 'desc' => 'openid'),
            ),
            "get_account_by_openid"     => array(
                'openid' => array('name' => 'openid', 'require' => true, 'desc' => 'openid'),
            ),
            "get_account_by_account"     => array(
                'username' => array('name' => 'username', 'require' => true, 'desc' => 'username'),
                'password' => array('name' => 'password', 'require' => true, 'desc' => 'password'),
            ),
        );
    }


    /**
     * 通过微信取帐号
     * @desc 多条件查询接口
     * @return array data 被查数据
     * @exception 400 非法请求，参数传递错误
     */
    public function get_account_by_account()
    {
        header('Access-Control-Allow-Origin:*');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header('Access-Control-Allow-Headers: *');

        $username = $this->username;
        $password = $this->password;

        //读出个人信息
        $userinfo = \PhalApi\DI()->notorm->rok_user
            ->where(array("username" => $username, "password"=>$password))->fetchOne();

        //如果有帐号
        if ($userinfo) {
            $account = \PhalApi\DI()->notorm->rok_account
                ->where(array("customer" => $userinfo['username'],))->fetchAll();
            \PhalApi\DI()->response->setMsg('success');
            return $account;
        }

    }


    /**
     * 通过微信取帐号
     * @desc 多条件查询接口
     * @return array data 被查数据
     * @exception 400 非法请求，参数传递错误
     */
    public function get_account_by_openid()
    {
        header('Access-Control-Allow-Origin:*');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header('Access-Control-Allow-Headers: *');

        $openid = $this->openid;

        //读出个人信息
        $userinfo = \PhalApi\DI()->notorm->rok_user
            ->where(array("openid" => $openid,))->fetchOne();

        //如果有帐号
        if ($userinfo) {
            $account = \PhalApi\DI()->notorm->rok_account
                ->where(array("customer" => $userinfo['username'],))->fetchAll();
            \PhalApi\DI()->response->setMsg('success');
            return $account;
        }

    }



    /**
     * 微信自动登录
     * @desc 多条件查询接口
     * @return array data 被查数据
     * @exception 400 非法请求，参数传递错误
     */
    public function Login()
    {
        header('Access-Control-Allow-Origin:*');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header('Access-Control-Allow-Headers: *');

        $openid = $this->openid;


        $userinfo = \PhalApi\DI()->notorm->rok_user
            ->where(
                array(
                    "openid" => $openid,
                )
            )->fetchOne();
        //如果有帐号
        if ($userinfo) {
            \PhalApi\DI()->response->setMsg('success');
            return '自动登录';
        }

    }

    /**
     * 通用多条件查询接口
     * @desc 多条件查询接口
     * @return array data 被查数据
     * @exception 400 非法请求，参数传递错误
     */
    public function change_WX()
    {
        header('Access-Control-Allow-Origin:*');

        $username = $this->username;
        $password = $this->password;
        $openid = $this->openid;

        $userinfo = \PhalApi\DI()->notorm->rok_user
            ->where(
                array(
                    "username" => $username,
                    "password" => $password,
                )
            )->fetchOne();
        //如果有帐号
        if ($userinfo) {
            \PhalApi\DI()->notorm->rok_user
                ->where(
                    'id', $userinfo['id']
                )->update(array("openid" => $openid));

            \PhalApi\DI()->response->setMsg('success');
            return '绑定成功';
        }

    }

}
